//package org.example.springbatchdemo.batch;
//
//import org.example.User;
//import org.example.springbatchdemo.PersonItemProcessor;
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.core.launch.support.RunIdIncrementer;
//import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
//import org.springframework.batch.item.database.JdbcBatchItemWriter;
//import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
//import org.springframework.batch.item.file.FlatFileItemReader;
//import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
//import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.ClassPathResource;
//import javax.sql.DataSource;
//
//@Configuration
//@EnableBatchProcessing // 开启 Spring Batch 功能
//public class BatchConfiguration {
//
//    @Autowired
//    public JobBuilderFactory jobBuilderFactory;
//
//    @Autowired
//    public StepBuilderFactory stepBuilderFactory;
//
//    // 1. 定义 Reader
//    @Bean
//    public FlatFileItemReader<User> reader() {
//        return new FlatFileItemReaderBuilder<User>()
//                .name("personItemReader") // 为Reader命名
//                .resource(new ClassPathResource("users.csv")) // 指定CSV文件路径
//                .delimited()
//                .names(new String[]{ "id","name","email"}) // CSV列名
//                .fieldSetMapper(new BeanWrapperFieldSetMapper<User>() {{
//                    setTargetType(User.class); // 将读取的行映射到Person对象
//                }})
//                .build();
//    }
//
//    // 2. 定义 Processor
//    @Bean
//    public PersonItemProcessor processor() {
//        return new PersonItemProcessor();
//    }
//
//    // 3. 定义 Writer
//    @Bean
//    public JdbcBatchItemWriter<User> writer(DataSource dataSource) {
//        return new JdbcBatchItemWriterBuilder<User>()
//                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
//                .sql("INSERT INTO user_db.`users` (id, name, email) VALUES (:id, :name, :email)") // SQL插入语句
//                .dataSource(dataSource) // 指定数据源
//                .build();
//    }
//
//    // 4. 定义 Job
//    @Bean
//    public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
//        return jobBuilderFactory.get("importUserJob")
//                .incrementer(new RunIdIncrementer()) // 使每次运行的Job实例都不同
//                .listener(listener) // 绑定监听器
//                .flow(step1) // 指定执行的步骤
//                .end()
//                .build();
//    }
//
//    // 5. 定义 Step
//    @Bean
//    public Step step1(JdbcBatchItemWriter<User> writer) {
//        return stepBuilderFactory.get("step1")
//                .<User, User> chunk(10) // 设置块大小为10。每次读取和处理10条记录，然后一次性写入
//                .reader(reader())
//                .processor(processor())
//                .writer(writer)
//                .build();
//    }
//}